package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;

public class ItemAction {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  public static final HashMap<Integer, ItemAction> _questionIdIndex = new HashMap<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  private static ItemAction _instance;
  
  public static ItemAction getInstance() {
    if (_instance == null)
      _instance = new ItemAction(); 
    return _instance;
  }
  
  public static void reload() {
    _questionIdIndex.clear();
    _instance = null;
    getInstance();
  }
  
  private ItemAction() {
    getData();
  }
  
  public static boolean forNpcQuest(String s, L1PcInstance pc, L1ItemInstance item) {
    ArrayList<Object> aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (((String)aTempData.get(0)).equals(s)) {
        String msg0 = "";
        String msg2 = "";
        String msg3 = "";
        String msg4 = "";
        String msg5 = "";
        String msg6 = "";
        String msg7 = "";
        String msg8 = "";
        String msg9 = "";
        String msg10 = "";
        String msg11 = "";
        String msg12 = "";
        String msg13 = "";
        String msg14 = "";
        String msg15 = "";
        String msg16 = "";
        String msg17 = "";
        String msg18 = "";
        String msg19 = "";
        String msg20 = "";
        String msg21 = "";
        String msg22 = "";
        if (((Integer)aTempData.get(1)).intValue() != 0) {
          msg0 = " " + ((Integer)aTempData.get(1)).intValue() + "级以上。 ";
        } else {
          msg0 = " 无限制 ";
        } 
        if (((Integer)aTempData.get(2)).intValue() == 1) {
          msg2 = " 王族";
        } else if (((Integer)aTempData.get(2)).intValue() == 2) {
          msg2 = " 骑士";
        } else if (((Integer)aTempData.get(2)).intValue() == 3) {
          msg2 = " 法师";
        } else if (((Integer)aTempData.get(2)).intValue() == 4) {
          msg2 = " 妖精";
        } else if (((Integer)aTempData.get(2)).intValue() == 5) {
          msg2 = " 黑妖";
        } else if (((Integer)aTempData.get(2)).intValue() == 0) {
          msg2 = " 所有职业";
        } 
        if (((Integer)aTempData.get(3)).intValue() != 0) {
          if (((Integer)aTempData.get(17)).intValue() != 0) {
            int j = ((Integer)aTempData.get(17)).intValue();
          } else {
            int j = ((Integer)aTempData.get(3)).intValue();
          } 
        } else {
          msg3 = "无限制";
        } 
        if ((String)aTempData.get(5) != null) {
          msg4 = ""+aTempData.get(5);
        } else {
          msg4 = "无标题";
        } 
        int HammerCount1 = 0;
        L1ItemInstance firehammer1 = pc.getInventory()
          .findItemId(((Integer)aTempData.get(18)).intValue());
        if (firehammer1 != null) {
          HammerCount1 = (int)firehammer1.getCount();
          if (HammerCount1 > ((Integer)aTempData.get(20)).intValue())
            HammerCount1 = ((Integer)aTempData.get(20)).intValue(); 
          if ((String)aTempData.get(19) != null && HammerCount1 > 0)
            msg22 = "[" + (String)aTempData.get(19) + "]成功机率+: " + HammerCount1 + " %"; 
        } 
        int[] materials = (int[])aTempData.get(6);
        int[] counts = (int[])aTempData.get(7);
        int[] enchants = (int[])aTempData.get(8);
        if (materials != null) {
          int j = 0;
          while (j < materials.length) {
            L1ItemInstance temp = ItemTable.get().createItem(materials[j]);
            temp.setEnchantLevel(enchants[j]);
            temp.setIdentified(true);
            switch (j) {
              case 0:
                msg5 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 1:
                msg6 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 2:
                msg7 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 3:
                msg8 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 4:
                msg9 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 5:
                msg10 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 6:
                msg11 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 7:
                msg12 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 8:
                msg13 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 9:
                msg14 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 10:
                msg15 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 11:
                msg16 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 12:
                msg17 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 13:
                msg18 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 14:
                msg19 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 15:
                msg19 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
              case 16:
                msg21 = String.valueOf(String.valueOf(temp.getLogName())) + " (" + counts[j] + ") 个";
                break;
            } 
            j++;
          } 
        } 
        if (((Integer)aTempData.get(4)).intValue() == 1)
          pc.getInventory().removeItem(item, 1L); 
        String[] msgs = { 
            msg0, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, 
            msg11, msg12, 
            msg13, msg14, msg15, msg16, msg17, msg18, msg19, "", 
            msg21, msg22 };
        pc.setitemactionhtml((String) aTempData.get(0));
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "ItemBlend3", msgs));
      } 
      if (pc.getitemactionhtml() == (String)aTempData.get(0) && ((String)aTempData.get(9)).equals(s)) {
        int[] materials2 = (int[])aTempData.get(6);
        int[] counts2 = (int[])aTempData.get(7);
        int[] enchants2 = (int[])aTempData.get(8);
        if (((Integer)aTempData.get(1)).intValue() != 0 && 
          pc.getLevel() < ((Integer)aTempData.get(1)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("等级必须为" + ((Integer)aTempData.get(1)).intValue() + "以上。"));
          return false;
        } 
        if (((Integer)aTempData.get(2)).intValue() != 0) {
          byte class_id = 0;
          String Classmsg = "";
          if (pc.isCrown()) {
            class_id = 1;
          } else if (pc.isKnight()) {
            class_id = 2;
          } else if (pc.isWizard()) {
            class_id = 3;
          } else if (pc.isElf()) {
            class_id = 4;
          } else if (pc.isDarkelf()) {
            class_id = 5;
          } else if (pc.isDragonKnight()) {
            class_id = 6;
          } else if (pc.isIllusionist()) {
            class_id = 7;
          } 
          switch (((Integer)aTempData.get(2)).intValue()) {
            case 1:
              Classmsg = "王族";
              break;
            case 2:
              Classmsg = "骑士";
              break;
            case 3:
              Classmsg = "法师";
              break;
            case 4:
              Classmsg = "妖精";
              break;
            case 5:
              Classmsg = "黑暗妖精";
              break;
          } 
          if (((Integer)aTempData.get(2)).intValue() != class_id) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "职业必须是", Classmsg, "才能制造此道具"));
            return false;
          } 
        } 
        boolean enough = true;
        int num = 0;
        int k = 0;
        while (k < materials2.length) {
          if (!pc.getInventory().checkEnchantItem(materials2[k], enchants2[k], counts2[k])) {
            L1Item temp2 = ItemTable.get().getTemplate(materials2[k]);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(temp2.getName())) + "(" + (
                  counts2[k] - pc.getInventory().countItems(temp2.getItemId())) + ")"));
            enough = false;
          } 
          k++;
        } 
        if (enough) {
          if (++num == materials2.length)
            enough = true; 
          if (enough) {
            int[] newcounts = new int[counts2.length];
            int l = 0;
            while (l < counts2.length) {
              newcounts[l] = counts2[l];
              pc.getInventory().consumeEnchantItem(materials2[l], enchants2[l], newcounts[l]);
              l++;
            } 
            int HammerCount2 = 0;
            L1ItemInstance firehammer2 = pc.getInventory()
              .findItemId(((Integer)aTempData.get(18)).intValue());
            if (firehammer2 != null) {
              HammerCount2 = (int)firehammer2.getCount();
              if (HammerCount2 > ((Integer)aTempData.get(20)).intValue())
                HammerCount2 = ((Integer)aTempData.get(20)).intValue(); 
            } 
            int TotalChance = ((Integer)aTempData.get(3)).intValue() + HammerCount2;
            Random _random = new Random();
            int New_itemid = ((Integer)aTempData.get(10)).intValue();
            int newcounts2 = ((Integer)aTempData.get(11)).intValue(), New_item_counts = newcounts2;
            if (_random.nextInt(100) >= TotalChance) {
              L1ItemInstance l1ItemInstance = null;
              if (l1ItemInstance.isStackable()) {
                l1ItemInstance.setEnchantLevel(((Integer)aTempData.get(12)).intValue());
                l1ItemInstance.setIdentified(true);
                l1ItemInstance.setCount(newcounts2);
                pc.getInventory().storeItem(l1ItemInstance);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(143, l1ItemInstance.getLogName()));
                if ((String)aTempData.get(13) != null && (
                  (Integer)aTempData.get(15)).intValue() == 1)
                  World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage(
                        String.format((String) aTempData.get(13), new Object[] { pc.getName(), l1ItemInstance.getLogName() }))); 
                if (((Integer)aTempData.get(16)).intValue() == 1)
                  pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId())); 
                enough = false;
              } else {
                L1ItemInstance newitem2 = ItemTable.get().createItem(New_itemid);
                newitem2.setEnchantLevel(((Integer)aTempData.get(12)).intValue());
                newitem2.setIdentified(true);
                newitem2.setCount(1L);
                pc.getInventory().storeItem(newitem2);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(143, newitem2.getLogName()));
                if ((String)aTempData.get(13) != null && (
                  (Integer)aTempData.get(15)).intValue() == 1)
                  World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage(
                        String.format((String) aTempData.get(13), new Object[] { pc.getName(), l1ItemInstance.getLogName() }))); 
                if (((Integer)aTempData.get(16)).intValue() == 1)
                  pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId())); 
                enough = false;
              } 
              return false;
            } 
            L1ItemInstance newitem = ItemTable.get().createItem(New_itemid);
            if (newitem != null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(""+aTempData.get(14)));
              if (((Integer)aTempData.get(16)).intValue() == 1)
                pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId())); 
              enough = false;
              return false;
            } 
          } 
        } 
      } 
      i++;
    } 
    return false;
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_道具火神制作");
      ArrayList<Object> aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          sTemp = rset.getString("action");
          aReturn.add(0, sTemp);
          aReturn.add(1, Integer.valueOf(rset.getInt("checkLevel_min")));
          aReturn.add(2, Integer.valueOf(rset.getInt("classid")));
          aReturn.add(3, new Integer(rset.getInt("成功机率")));
          aReturn.add(4, new Integer(rset.getInt("是否扣除道具")));
          aReturn.add(5, rset.getString("制造清单名称"));
          if (rset.getString("需求物品") != null && !rset.getString("需求物品").equals("") && 
            !rset.getString("需求物品").equals("0")) {
            aReturn.add(6, getArray(rset.getString("需求物品"), ",", 1));
          } else {
            aReturn.add(6, null);
          } 
          if (rset.getString("需求物品数量") != null && !rset.getString("需求物品数量").equals("") && 
            !rset.getString("需求物品数量").equals("0")) {
            aReturn.add(7, getArray(rset.getString("需求物品数量"), ",", 1));
          } else {
            aReturn.add(7, null);
          } 
          aReturn.add(8, getArray(rset.getString("需求物品强化值"), ",", 1));
          aReturn.add(9, rset.getString("确认制作action"));
          aReturn.add(10, Integer.valueOf(rset.getInt("给予道具")));
          aReturn.add(11, Integer.valueOf(rset.getInt("给予道具数量")));
          aReturn.add(12, Integer.valueOf(rset.getInt("给予道具强化值")));
          aReturn.add(13, rset.getString("制作成功"));
          aReturn.add(14, rset.getString("制作失败"));
          aReturn.add(15, new Integer(rset.getInt("世界广播")));
          aReturn.add(16, new Integer(rset.getInt("是否关闭对话视窗")));
          aReturn.add(17, Integer.valueOf(rset.getInt("显示假机率")));
          aReturn.add(18, new Integer(rset.getInt("自订机率道具")));
          aReturn.add(19, rset.getString("自订机率道具名称"));
          aReturn.add(20, new Integer(rset.getInt("自订机率数量上限")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
